/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package kn.cstf2.global.Utils;

import java.io.*;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.logging.Level;
//import java.util.logging.Logger;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.global.db.Operations.OperacionesDB;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import java.util.logging.Level;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.transition.Server.TransitionServer;
import net.sf.jasperreports.view.JasperViewer;

//import org.apache.log4j.Logger;


/**
 *
 * @author david.dominguez
 */
public class MyReportCreator {
    private static final String DEFAULT = "PDF";
    private static final String PDF = "PDF";
    private static final String HTML = "HTML";
    private static final String EXCEL = "XLS";
    private String path = "/Reportes";
    private SimpleDateFormat sdf_fileName = new SimpleDateFormat("yyyyMMddHHmm");

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }
 
    
    public InputStream  generarReporte(Integer knkey) {
    InputStream fis = null;

        try {
            HashMap<String, Object> reportParams = new HashMap<String, Object>();   
            reportParams.put("knkey", knkey);
            String outFileName =  "logy"+sdf_fileName.format(Calendar.getInstance().getTime())+".pdf";
            fis = createReport("ReportCstf2.jrxml", reportParams, PDF, outFileName);
            
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(MyReportCreator.class.getName()).log(Level.SEVERE, null, ex);
        }
        return fis;        
    }
    
    private InputStream createReport(String reportName, HashMap<String, Object> reportParams, String outputFormat, String outFileName) throws Exception{

            InputStream fis = null;
            OperacionesDB bdOps = new OperacionesDB();

            try{
                InputStream reportTemplate = this.getClass().getResourceAsStream("/kn/cstf2/transition/Server/ReportCstf2.jrxml");
                String testRuta ="/ReportCstf2.jrxml";
                JasperDesign design = JRXmlLoader.load(reportTemplate);
                Class.forName("org.postgresql.Driver");
                Connection conn = bdOps.obtenerConexion("db");
                JasperReport report = JasperCompileManager.compileReport(design);
                JasperPrint print = JasperFillManager.fillReport(report, reportParams, conn);

               byte[] bytes =  JasperExportManager.exportReportToPdf(print);               

               fis = new ByteArrayInputStream(bytes);
              
               conn.close();

            }catch(JRException e){
                
                throw new CstF2Exception(e);
            }
            return fis;
    }
}